package com.imti.fragmentdemo;

import android.os.Bundle;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ThridFragment extends Fragment
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();

        if (bundle != null)
        {
            String data = bundle.getString("activity_data");

            //getActivity()和requireActivity()效果相同
            //区别在于getActivity()有一定概率返回null
            Toast.makeText(requireActivity(), data,
                Toast.LENGTH_LONG).show();
        }
    }

    private void getData()
    {
        //Fragment获取Activity数据
        //通过直接调用Activity的public方法的前提是：
        //Fragment是通过动态加载的方式加载到Activity中的
        String data = ((MainActivity) requireActivity()).sendData();
    }

    public String sendMsg()
    {
        return "来自Fragment的数据";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_thrid, container, false);
    }
}